#!/bin/bash
[% c("var/set_default_env") -%]

# This project is expected to be built only on Linux.
# Then the actual Go compiler will be built for each platform.

distdir=/var/tmp/dist/[% project %]
mkdir -p /var/tmp/build /var/tmp/dist

# Building go 1.4.x
# This is needed to bootstrap the go that we actually use
# https://golang.org/doc/install/source#go14
tar -C /var/tmp/build --transform='s,^go\>,go1.4,' -xf $rootdir/[% c('input_files_by_name/go14') %]
cd /var/tmp/build/go1.4/src
patch -p2 < $rootdir/go-aarch64.patch
# Disable cgo to avoid conflicts with newer GCC. cgo is not needed for the bootstrap go.
# https://github.com/golang/go/issues/13114#issuecomment-186922245
# Disable CC etc. that are set up for cross builds.
export GOOS=linux
export GOHOSTARCH=[% c("var/GOARCH14") %]
export GOARCH=[% c("var/GOARCH14") %]
CGO_ENABLED=0 CC= CFLAGS= LDFLAGS= ./make.bash
export GOROOT_BOOTSTRAP="/var/tmp/build/go1.4"

cd $rootdir

tar -C /var/tmp/dist -xf $rootdir/[% c('input_files_by_name/go') %]
export GOROOT="$distdir"
mv /var/tmp/dist/go $distdir
cd $distdir/src

export GOHOSTARCH=[% c("var/GOARCH") %]
export GOARCH=[% c("var/GOARCH") %]
./make.bash

# This directory is non-reproducible and doesn't exist in official Go releases,
# so remove it to preserve reproducibility of the output.
rm -rf $distdir/pkg/obj/go-build/

cd /var/tmp/dist
[% c('tar', {
        tar_src => [ project ],
        tar_args => '-caf ' _ dest_dir _ '/' _ c('filename'),
    }) %]
